package live.zs.services;

import io.quarkus.runtime.StartupEvent;
import io.vertx.ext.consul.ConsulClientOptions;
import io.vertx.ext.consul.ServiceOptions;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.ext.consul.ConsulClient;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;

/**
 * @author madison
 * @description
 * @date 2022/2/10 18:10
 */
@ApplicationScoped
public class Registration {
    @ConfigProperty(name = "consul.host")
    String host;
    @ConfigProperty(name = "consul.port")
    int port;

    @ConfigProperty(name = "blue-service-port", defaultValue = "9000")
    int red;
    @ConfigProperty(name = "red-service-port", defaultValue = "9001")
    int blue;

    public void init(@Observes StartupEvent ev, Vertx vertx) {
        ConsulClient client = ConsulClient.create(vertx, new ConsulClientOptions().setHost(host).setPort(port));
        client.registerServiceAndAwait(new ServiceOptions().setPort(blue).setAddress("localhost").setName("my-service").setId("blue"));
        client.registerServiceAndAwait(new ServiceOptions().setPort(red).setAddress("localhost").setName("my-service").setId("blue"));
    }
}
